`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Module: AUDIO_TOP(OLD Ver)                                                   //
// Author: Zhiyuan Lin                                                          //
// Date: 3/30/2014                                                              //
// Description: This module is the TOP module that combines everything.         //
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// NOTE: Synthesize pass, Implement Design pass.                                //
// Test: PASS, but need to modify the Tunes and frequencies.                    //
//       3/31/2014                                                              //
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// UPDATE: Add pause function 4/1/2014                                          //
//////////////////////////////////////////////////////////////////////////////////
module AUDIO_TOP(CLK, Speaker, LED, Tone, Play_END,
                Sim_Song1, Sim_Song2, Sim_Song3, 
	             Sim_Pause, Sim_Turbo, Sim_Start, Sim_Restart,
			       Sim_Btn1, Sim_Btn2, Sim_Btn3, Sim_Btn4);
    
	 input CLK,  
	       Sim_Song1, Sim_Song2, Sim_Song3, 
	       Sim_Pause, Sim_Turbo, Sim_Start, Sim_Restart,
			 Sim_Btn1, Sim_Btn2, Sim_Btn3, Sim_Btn4;
			 
	 output Speaker, Play_END;
	 output [7:0] LED;
	 output [5:0] Tone;
	 wire [5:0] Tone1, Tone2, Tone3;
	 wire CLK_DIV, Tune1, Tune2, Tune3;
	 wire Play_END1, Play_END2, Play_END3;
						 
AUDIO_CLK_DIV M1 (CLK_DIV, CLK, Sim_Pause, Sim_Turbo, Sim_Start);
AUDIO_TUNE M21 (Tune1, CLK_DIV, Tone1, Sim_Restart, Play_END1);
AUDIO_TUNE2 M22 (Tune2, CLK_DIV, Tone2, Sim_Restart, Play_END2);
AUDIO_TUNE3 M23 (Tune3, CLK_DIV, Tone3, Sim_Restart, Play_END3);

/*assign Speaker = ((Sim_Song1 == 1'b1) && (Sim_Start == 1'b1))? Tune1 :
                 ((Sim_Song2 == 1'b1) && (Sim_Start == 1'b1))? Tune2 : 
					  ((Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))? Tune3 :1'b0;
					 */ 
					  				  
assign Speaker = 
(
((Sim_Song1 == 1'b1) && (Sim_Song2 == 1'b1) && (Sim_Start == 1'b1)) || 
((Sim_Song1 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1)) ||
((Sim_Song2 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1)) ||
((Sim_Song1 == 1'b1) && (Sim_Song2 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))
)? 1'b0 :
((Sim_Song1 == 1'b1) && (Sim_Start == 1'b1))? Tune1 :
((Sim_Song2 == 1'b1) && (Sim_Start == 1'b1))? Tune2 : 
((Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))? Tune3 :1'b0;	

					  
assign Tone = 
(
((Sim_Song1 == 1'b1) && (Sim_Song2 == 1'b1) && (Sim_Start == 1'b1)) || 
((Sim_Song1 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1)) ||
((Sim_Song2 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1)) ||
((Sim_Song1 == 1'b1) && (Sim_Song2 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))
)? 5'b0 :
((Sim_Song1 == 1'b1) && (Sim_Start == 1'b1))? Tone1 :
((Sim_Song2 == 1'b1) && (Sim_Start == 1'b1))? Tone2 : 
((Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))? Tone3 :5'b0;	

				
assign Play_END = 
(
((Sim_Song1 == 1'b1) && (Sim_Song2 == 1'b1) && (Sim_Start == 1'b1)) || 
((Sim_Song1 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1)) ||
((Sim_Song2 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1)) ||
((Sim_Song1 == 1'b1) && (Sim_Song2 == 1'b1) && (Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))
)? 1'b0 :
((Sim_Song1 == 1'b1) && (Sim_Start == 1'b1))? Play_END1 :
((Sim_Song2 == 1'b1) && (Sim_Start == 1'b1))? Play_END2 : 
((Sim_Song3 == 1'b1) && (Sim_Start == 1'b1))? Play_END3 :1'b0;	
	  
					  				  
assign LED[7:6] = Sim_Btn1 ? 2'b11 : 2'b00; 
assign LED[5:4] = Sim_Btn2 ? 2'b11 : 2'b00;   
assign LED[3:2] = Sim_Btn3 ? 2'b11 : 2'b00;   
assign LED[1] = Sim_Btn4 ? 'b1 : 'b0; 
assign LED[0] = Play_END ? 'b1 : 'b0;    
				   
endmodule
